const express = require("express");
const { ObjectId } = require("mongodb");
const {
  insertMany,
  insertOne,
  find,
  update,
  updateMany,
  distinct,
  deleteMany,
  extendFunc,
} = require("./mongo");
const app = express();
const port = 3000;

app.get("/", async (req, res) => {
  const id = new ObjectId("62be9c105e44bd261d50f79c");
  //   const result = await find("工号",{ _id: id });
  //   const result = await find("工号",{ '姓名': '吴红华' });
  //   const result = await find("工号",{ '_id': '62be9c105e44bd261d50f79c' });
  const result = await update(
    "工号",
    { _id: "62be9c105e44bd261d50f79c" },
    { $set: { 班组: "abc123" } }
  );
  console.log(result);
  res.send("Hello World!");
});

app.get('/test',async(req,res)=>{
  const result=await find('工号')
  res.send(result)
})

app.get("/:name/:part", async (req, res) => {
  if (!users) {
    console.log("用户数据为空");
    res.send("用户数据为空");
    return;
  }
  const name=req.params["name"]
  const part=req.params["part"]
  const cur_user=users.find(u=>u.姓名==name)
  if(cur_user){
    const result = await update(
      "工号",
      { _id: cur_user._id },
      { $set: { 班组: part } }
    );
    res.send(result)
  }else{
    console.log(name+'查询不到信息');
    res.send(name+'查询不到信息');
  }
  // console.log( + ":" + req.params["bz"]);
  // res.send(req.params["name"] + ":" + req.params["bz"]);
});

let users = null;

app.listen(port, async () => {
  //初始化数据
  users = await find("工号");
  console.log(`Example app listening on port ${port}`);
});
